package me.luger.project.picc.service;

import me.luger.core.commom.util.DateUtil;
import me.luger.core.commom.util.StringUtil;
import me.luger.core.dao.cache.redis.springdata.RedisManager;
import me.luger.core.dao.nutz.entity.page.DataTableRequest;
import me.luger.core.dao.nutz.entity.page.DataTableResult;
import me.luger.core.dao.nutz.service.BaseService;
import me.luger.project.picc.entity.*;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Repository
public class PrizeListService extends BaseService<PrizeList> {

    @Autowired
    private SatisfyQuestionService satisfyQuestionService;

    @Autowired
    private LearnHealthService learnHealthService;

    @Autowired
    private InsuranceService insuranceService;

    @Autowired
    private PiccMeetingService piccMeetingService;

    @Autowired
    RedisManager redisManager;

    public PrizeListService(Dao dao) {
        super(dao);
    }

    /**
     * 根据微信openId和活动ID获取奖品信息
     * @param openId
     * @param typeId
     * @return
     */
    public PrizeList fetchByOpenIdAndType(String openId,int typeId){
        PrizeList prizeList = this.fetchOne(Cnd.where("typeId","=",typeId).and("openId","=",openId).orderBy("id","desc"));
        return prizeList;
    }

    public PrizeList fetchByOpenIdAndTypeAndBizId(String openId,int typeId,int bizId){
        PrizeList prizeList = this.fetchOne(Cnd.where("typeId","=",typeId).and("openId","=",openId).and("bizId","=",bizId));
        return prizeList;
    }

    public PrizeList fetchBySecrect(String openId,String prizeSecrect,int typeId){
        PrizeList prizeList = this.fetchOne(Cnd.where("typeId","=",typeId).and("openId","=",openId).and("prizeSecrect","=",prizeSecrect));
        return prizeList;
    }

    /**
     * 判断用户是否抽过奖
     * @param openId
     * @param typeId
     * @return
     */
    public boolean checkUserDoDraw(String openId,int typeId){
        PrizeList prizeList = this.fetchOne(Cnd.where("typeId","=",typeId).and("openId","=",openId));
        if(null == prizeList){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 判断用户是否中奖
     * @param openId
     * @param typeId
     * @return
     */
    public boolean checkPrizeResultByOpenIdAndType(String openId,int typeId){
        PrizeList prizeList = this.fetchOne(Cnd.where("typeId","=",typeId).and("openId","=",openId).and("prizeResult","=","1"));
        if(null == prizeList){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 用户抽奖
     * @param bizId
     * @param typeId
     * @return
     */
    public PrizeList doDraw(Integer bizId, int typeId) {
        PrizeList prizeList = null;
        switch (typeId){
            case 1://1：客户满意度有奖调查
                prizeList = satisfyQuestionDoDraw1(bizId);
                break;
            case 2://2：学点健康
                prizeList = learnHealthDoDraw1(bizId);
                break;
            case 3://3：投保有奖
                prizeList = insuranceDoDraw1(bizId);
                break;
            default:
                return null;
        }
        return prizeList;
    }


    private PrizeList insuranceDoDraw1(Integer bizId) {
        Date now = DateUtil.nowAsDate();
        String nowDateFmt = DateUtil.formatNow(DateUtil.DATE_YYYYMMDD);
        Insurance insurance = insuranceService.fetchById(bizId);
        String pid = insurance.getPid();
        String openId = insurance.getOpenId();
        int doPrizeStatus = insurance.getDoPrizeStatus();

        int typeId = ActivityEnum.INSURANCE.getActivityId();
        int prizeId = 999;
        String prizeDetail = "", typeName = ActivityEnum.INSURANCE.getName();
        int prizeResult = 0,jiaodu = 0;

        boolean hasGetPrize = false;

        if(isGetPrizeByPid(pid) || isInInsurancePrize(pid) || doPrizeStatus == 0){
            prizeId = 999;
            prizeDetail = "谢谢您的参与";
            prizeResult = 0;
            hasGetPrize = true;//代表抽奖已结束
            Integer[] arr = {54, 125, 198, 270, 342};
            jiaodu =  arr[new Random().nextInt(5)];
        }else{
            setOpenId2Prize(openId);
            setPid2Prize(pid);
            if(doPrizeStatus == 1){
                redisManager.incr("prize:insurance:canGetPrize1:"+nowDateFmt);
            }else if(doPrizeStatus == 2){
                redisManager.incr("prize:insurance:canGetPrize2:"+nowDateFmt);
            }

        }



        String canGetPrizeRank1Str = redisManager.get("prize:insurance:canGetPrize1:"+nowDateFmt);
        String canGetPrizeRank2Str = redisManager.get("prize:insurance:canGetPrize2:"+nowDateFmt);
        int canGetPrizeRank1 = 1,canGetPrizeRank2 = 1;
        if(null != canGetPrizeRank1Str && StringUtil.isNumeric(canGetPrizeRank1Str)){
            canGetPrizeRank1 = Integer.parseInt(canGetPrizeRank1Str);
        }
        if(null != canGetPrizeRank2Str && StringUtil.isNumeric(canGetPrizeRank2Str)){
            canGetPrizeRank2 = Integer.parseInt(canGetPrizeRank2Str);
        }


        //具备中奖资质
        if(!hasGetPrize && doPrizeStatus == 1) {
            setInsurancePrize(pid);//表示在该活动过抽过奖了，后面不能在抽奖了


//            if(canGetPrizeRank1 == 1 && DateUtil.isCurrentDay(2017,5,15)){
//                prizeId = 0;
//                prizeDetail = "Apple iPad 9.7英寸（128G）金色";
//                prizeResult = 1;
//                hasGetPrize = true;
//                jiaodu = 18;
//            }
//            if(canGetPrizeRank1 == 2 && DateUtil.isCurrentDay(2017,5,15)){
//                prizeId = 1;
//                prizeDetail = "小米（MI）车载空气净化器";
//                prizeResult = 1;
//                hasGetPrize = true;
//                jiaodu = 234;
//            }
//            if(canGetPrizeRank1 == 3 && DateUtil.isCurrentDay(2017,5,15)){
//                prizeId = 2;
//                prizeDetail = "美固 车载小冰箱 7L";
//                prizeResult = 1;
//                hasGetPrize = true;
//                jiaodu = 90;
//            }
//            if(canGetPrizeRank1 == 4 && DateUtil.isCurrentDay(2017,5,15)){
//                prizeId = 3;
//                prizeDetail = "小米充电宝";
//                prizeResult = 1;
//                hasGetPrize = true;
//                Integer[] arr = {162,306};
//                jiaodu =  arr[new Random().nextInt(1)];
//            }

            /**
             * 特等奖
             * 8月20日、8月25日、8月29日每日出奖1个，出奖方式为参与本活动的符合中奖条件的第8位用户
             */
            if(canGetPrizeRank1 == 8){
                if(DateUtil.isCurrentDay(2017,8,20) || DateUtil.isCurrentDay(2017,8,25) || DateUtil.isCurrentDay(2017,8,29)){
                    prizeId = 0;
                    prizeDetail = "Apple iPad 9.7英寸（128G）金色";
                    prizeResult = 1;
                    hasGetPrize = true;
                    jiaodu = 18;
                }
            }

            /**
             * 一等奖
             * 8月20日-8月29日每日出奖5个，出奖方式为参与本活动的符合中奖条件的第6、10、16、18、25位用户
             */
            if(canGetPrizeRank1 == 6 || canGetPrizeRank1 == 10 || canGetPrizeRank1 == 16 || canGetPrizeRank1 == 18 || canGetPrizeRank1 == 25){
                if(DateUtil.isInBetweenDate(DateUtil.newDate(2017,8,20),DateUtil.newDate(2017,8,30))){
                    prizeId = 1;
                    prizeDetail = "小米（MI）车载空气净化器";
                    prizeResult = 1;
                    hasGetPrize = true;
                    jiaodu = 234;
                }
            }

            /**
             * 二等奖
             * 8月20日出奖10个，出奖方式为参与本活动的符合中奖条件的第1、3、5、7、9、21、23、25、27、29位用户获奖，
             */
            if(canGetPrizeRank1 == 1 || canGetPrizeRank1 == 3 || canGetPrizeRank1 == 5
                    || canGetPrizeRank1 == 7 || canGetPrizeRank1 == 9 || canGetPrizeRank1 == 21 || canGetPrizeRank1 == 23
                    || canGetPrizeRank1 == 25 || canGetPrizeRank1 == 27 || canGetPrizeRank1 == 29) {
                if(DateUtil.isCurrentDay(2017,8,20)){
                    prizeId = 2;
                    prizeDetail = "美固 车载小冰箱 7L";
                    prizeResult = 1;
                    hasGetPrize = true;
                    jiaodu = 90;
                }
            }

            /**
             * 二等奖
             * 8月21日-8月30日，每日出奖9个，出奖方式为参与本活动的符合中奖条件的第1、3、5、7、9、21、23、25、27、用户中奖
             */
            if(canGetPrizeRank1 == 1 || canGetPrizeRank1 == 3 || canGetPrizeRank1 == 5
                    || canGetPrizeRank1 == 7 || canGetPrizeRank1 == 9 || canGetPrizeRank1 == 21 || canGetPrizeRank1 == 23
                    || canGetPrizeRank1 == 25 || canGetPrizeRank1 == 27) {
                if(DateUtil.isInBetweenDate(DateUtil.newDate(2017,8,21),DateUtil.newDate(2017,8,31))){
                    prizeId = 2;
                    prizeDetail = "美固 车载小冰箱 7L";
                    prizeResult = 1;
                    hasGetPrize = true;
                    jiaodu = 90;
                }
            }

            /**
             * 三等奖
             * 8月20日-8月25日每日出奖50个，出奖方式为随机；8月26日-8月30日每日出奖40个，出奖方式为随机
             */
            if(!hasGetPrize){
                if(DateUtil.isInBetweenDate(DateUtil.newDate(2017,8,20),DateUtil.newDate(2017,8,26))){
                    int upLimit1 = 0;
                    String upLimit1Str = redisManager.get("prize:insurance:3:limit1:"+nowDateFmt);

                    if(StringUtil.isNotBlank(upLimit1Str) && StringUtil.isNumeric(upLimit1Str)){
                        upLimit1 = Integer.parseInt(upLimit1Str);
                    }
                    if(upLimit1 < 50 && new Random().nextInt(50) == 25){
                        prizeId = 3;
                        prizeDetail = "小米充电宝";
                        prizeResult = 1;
                        hasGetPrize = true;
                        redisManager.incr("prize:insurance:3:limit1:"+nowDateFmt);
                        Integer[] arr = {162,306};
                        jiaodu =  arr[new Random().nextInt(1)];
                    }
                }else if(DateUtil.isInBetweenDate(DateUtil.newDate(2017,8,26),DateUtil.newDate(2017,8,31))){
                    int upLimit2 = 0;
                    String upLimit2Str = redisManager.get("prize:insurance:3:limit2:"+nowDateFmt);

                    if(StringUtil.isNotBlank(upLimit2Str) && StringUtil.isNumeric(upLimit2Str)){
                        upLimit2 = Integer.parseInt(upLimit2Str);
                    }
                    if(upLimit2 < 40 && new Random().nextInt(40) == 20){
                        prizeId = 3;
                        prizeDetail = "小米充电宝";
                        prizeResult = 1;
                        hasGetPrize = true;
                        redisManager.incr("prize:insurance:3:limit2:"+nowDateFmt);
                        Integer[] arr = {162,306};
                        jiaodu =  arr[new Random().nextInt(1)];
                    }
                }
            }
        }else if(!hasGetPrize && doPrizeStatus == 2) {//具备中奖资质
            setInsurancePrize(pid);//表示在该活动过抽过奖了，后面不能在抽奖了
            /**
             * 特等奖
             * 8月22日、8月27日、8月30日每日出奖1个，出奖方式为参与本活动的符合中奖条件的第8位用户
             */
            if(canGetPrizeRank2 == 8){
                if(DateUtil.isCurrentDay(2017,8,22)
                        || DateUtil.isCurrentDay(2017,8,27)
                        || DateUtil.isCurrentDay(2017,8,30)){
                    prizeId = 0;
                    prizeDetail = "小米室内空气净化器";
                    prizeResult = 1;
                    hasGetPrize = true;
                    jiaodu = 18;
                }
            }

            /**
             * 一等奖
             * 8月20日-8月29日每日出奖3个，出奖方式为参与本活动的符合中奖条件的第8、20、30位用户
             */
            if(canGetPrizeRank2 == 8 || canGetPrizeRank2 == 20 || canGetPrizeRank2 == 30 ){
                if(DateUtil.isInBetweenDate(DateUtil.newDate(2017,8,20),DateUtil.newDate(2017,8,30))){
                    prizeId = 1;
                    prizeDetail = "小米空气PM2.5检测仪霾表";
                    prizeResult = 1;
                    hasGetPrize = true;
                    jiaodu = 234;
                }
            }

            /**
             * 二等奖
             * 8月20日出奖10个，出奖方式为参与本活动的符合中奖条件的第6、8、12、16、20、31、35、40、45、50位用户获奖
             */
            if(canGetPrizeRank2 == 6 || canGetPrizeRank2 == 8 || canGetPrizeRank2 == 12
                    || canGetPrizeRank2 == 16 || canGetPrizeRank2 == 20 || canGetPrizeRank2 == 31 || canGetPrizeRank2 == 35
                    || canGetPrizeRank2 == 40 || canGetPrizeRank2 == 45 || canGetPrizeRank2 == 50) {
                if(DateUtil.isCurrentDay(2017,8,20)){
                    prizeId = 2;
                    prizeDetail = "黑科技智能电源蓝牙音响";
                    prizeResult = 1;
                    hasGetPrize = true;
                    jiaodu = 90;
                }
            }

            /**
             * 二等奖
             * 8月21日-8月30日，每日出奖9个，出奖方式为参与本活动的符合中奖条件的第6、8、12、16、20、31、35、40、45用户中奖。
             */
            if(canGetPrizeRank2 == 6 || canGetPrizeRank2 == 8 || canGetPrizeRank2 == 12
                    || canGetPrizeRank2 == 16 || canGetPrizeRank2 == 20 || canGetPrizeRank2 == 31 || canGetPrizeRank2 == 35
                    || canGetPrizeRank2 == 40 || canGetPrizeRank2 == 45) {
                if(DateUtil.isInBetweenDate(DateUtil.newDate(2017,8,21),DateUtil.newDate(2017,8,31))){
                    prizeId = 2;
                    prizeDetail = "黑科技智能电源蓝牙音响";
                    prizeResult = 1;
                    hasGetPrize = true;
                    jiaodu = 90;
                }
            }

            /**
             * 三等奖
             * 8月20日-8月25日每日出奖20个，出奖方式为随机；8月26日-8月30日每日出奖36个，出奖方式为随机
             */
            if(!hasGetPrize){
                if(DateUtil.isInBetweenDate(DateUtil.newDate(2017,8,20),DateUtil.newDate(2017,8,26))){
                    int upLimit1 = 0;
                    String upLimit1Str = redisManager.get("prize:insurance:3:limit3:"+nowDateFmt);

                    if(StringUtil.isNotBlank(upLimit1Str) && StringUtil.isNumeric(upLimit1Str)){
                        upLimit1 = Integer.parseInt(upLimit1Str);
                    }
                    if(upLimit1 < 20 && new Random().nextInt(50) == 25){
                        prizeId = 3;
                        prizeDetail = "小米充电宝";
                        prizeResult = 1;
                        hasGetPrize = true;
                        redisManager.incr("prize:insurance:3:limit3:"+nowDateFmt);
                        Integer[] arr = {162,306};
                        jiaodu =  arr[new Random().nextInt(1)];
                    }
                }else if(DateUtil.isInBetweenDate(DateUtil.newDate(2017,8,26),DateUtil.newDate(2017,8,31))){
                    int upLimit2 = 0;
                    String upLimit2Str = redisManager.get("prize:insurance:3:limit4:"+nowDateFmt);

                    if(StringUtil.isNotBlank(upLimit2Str) && StringUtil.isNumeric(upLimit2Str)){
                        upLimit2 = Integer.parseInt(upLimit2Str);
                    }
                    if(upLimit2 < 36 && new Random().nextInt(40) == 20){
                        prizeId = 3;
                        prizeDetail = "小米充电宝";
                        prizeResult = 1;
                        hasGetPrize = true;
                        redisManager.incr("prize:insurance:3:limit4:"+nowDateFmt);
                        Integer[] arr = {162,306};
                        jiaodu =  arr[new Random().nextInt(1)];
                    }
                }
            }
        }

        if(StringUtil.isBlank(prizeDetail)){
            prizeId = 999;
            prizeDetail = "谢谢您的参与";
            prizeResult = 0;
            Integer[] arr = {54, 125, 198, 270, 342};
            jiaodu =  arr[new Random().nextInt(5)];
        }


        if(0<=prizeId && prizeId <=3){
            setOpenId2Prize(openId);
            setPid2Prize(pid);
        }

        PrizeList prizeList = new PrizeList();
        prizeList.setOpenId(insurance.getOpenId());
        prizeList.setPid(insurance.getPid());
        prizeList.setTypeId(typeId);
        prizeList.setPrizeId(prizeId);
        prizeList.setPrizeResult(prizeResult);
        prizeList.setPrizeDetail(prizeDetail);
        prizeList.setPrizeSecrect(StringUtil.randomSecret());
        prizeList.setPrizeDate(DateUtil.nowAsDate());
        prizeList.setTypeName(typeName);
        prizeList.setBizId(bizId);
        prizeList.setJiaodu(jiaodu);
        prizeList = this.insert(prizeList);
        return prizeList;

    }

    private PrizeList learnHealthDoDraw1(Integer bizId) {
        Date now = DateUtil.nowAsDate();
        String nowDateFmt = DateUtil.formatNow(DateUtil.DATE_YYYYMMDD);
        LearnHealth learnHealth = learnHealthService.fetchById(bizId);
        String openId = learnHealth.getOpenId();
        int doPrizeStatus = learnHealth.getDoPrizeStatus();

        int typeId = ActivityEnum.LEARN_HEALTH.getActivityId();
        int prizeId = 999;
        String prizeDetail = "",typeName = ActivityEnum.LEARN_HEALTH.getName();
        int prizeResult = 0,jiaodu=0;

        boolean hasGetPrize = false;

        if(isGetPrizeByOpenId(openId) || StringUtil.isBlank(openId) || doPrizeStatus == 0){
            prizeId = 999;
            prizeDetail = "谢谢您的参与";
            prizeResult = 0;
            hasGetPrize = true;//代表抽奖已结束
            Integer[] arr = {54, 125, 198, 270, 342};
            jiaodu =  arr[new Random().nextInt(5)];
        }else{
            setOpenId2Prize(openId);
            redisManager.incr("prize:learnHealth:canGetPrize:"+nowDateFmt);
        }



        String canGetPrizeRankStr = redisManager.get("prize:learnHealth:canGetPrize:"+nowDateFmt);
        int canGetPrizeRank = 1;
        if(null != canGetPrizeRankStr && StringUtil.isNumeric(canGetPrizeRankStr)){
            canGetPrizeRank = Integer.parseInt(canGetPrizeRankStr);
        }

        /**
         * 一等奖
         * 5月19日、6月3日、6月17日、6月27日、7月4日、7月17日出奖方式为每天参与本活动的符合中奖条件的第一位用户
         */
        if(!hasGetPrize){
            if(canGetPrizeRank == 1){
                if(DateUtil.isCurrentDay(2017,5,19)||
                        DateUtil.isCurrentDay(2017,6,3)||
                        DateUtil.isCurrentDay(2017,6,17)||
                        DateUtil.isCurrentDay(2017,6,27)||
                        DateUtil.isCurrentDay(2017,7,4)||
                        DateUtil.isCurrentDay(2017,7,17)
                        ){
                    prizeId = 1;
                    prizeDetail = "“人民健康”app健康商城积分10000";
                    prizeResult = 1;
                    hasGetPrize = true;
                    jiaodu =  18;
                }
            }
        }


        /**
         * 二等奖
         * 5月18日，5月20日，5月22日，5月24日，5月26日，5月28日，5月30日， 6月1日，6月4日，6月7日，6月9日，6月11日，6月13日，6月16日，6月18日，
         * 6月20日，6月22日，6月24日，6月26日，6月28日，，6月29日，7月1日，7月3日，7月5日，7月8日，7月10日，7月12日，7月14日，7月16日，7月18日，
         * 每日一个，出奖方式为每日参与本活动的符合条件的第6位用户中奖
         */
        if(!hasGetPrize){
            if(canGetPrizeRank == 6){
                if(DateUtil.isCurrentDay(2017,5,18) ||
                        DateUtil.isCurrentDay(2017,5,20)||
                        DateUtil.isCurrentDay(2017,5,22)||
                        DateUtil.isCurrentDay(2017,5,24)||
                        DateUtil.isCurrentDay(2017,5,26)||
                        DateUtil.isCurrentDay(2017,5,28)||
                        DateUtil.isCurrentDay(2017,5,30)||
                        DateUtil.isCurrentDay(2017,6,1)||
                        DateUtil.isCurrentDay(2017,6,4)||
                        DateUtil.isCurrentDay(2017,6,7)||
                        DateUtil.isCurrentDay(2017,6,9)||
                        DateUtil.isCurrentDay(2017,6,11)||
                        DateUtil.isCurrentDay(2017,6,13)||
                        DateUtil.isCurrentDay(2017,6,16)||
                        DateUtil.isCurrentDay(2017,6,18)||
                        DateUtil.isCurrentDay(2017,6,20)||
                        DateUtil.isCurrentDay(2017,6,22)||
                        DateUtil.isCurrentDay(2017,6,24)||
                        DateUtil.isCurrentDay(2017,6,26)||
                        DateUtil.isCurrentDay(2017,6,28)||
                        DateUtil.isCurrentDay(2017,6,29)||
                        DateUtil.isCurrentDay(2017,7,1)||
                        DateUtil.isCurrentDay(2017,7,3)||
                        DateUtil.isCurrentDay(2017,7,5)||
                        DateUtil.isCurrentDay(2017,7,8)||
                        DateUtil.isCurrentDay(2017,7,10)||
                        DateUtil.isCurrentDay(2017,7,12)||
                        DateUtil.isCurrentDay(2017,7,14)||
                        DateUtil.isCurrentDay(2017,7,16)||
                        DateUtil.isCurrentDay(2017,7,18)
                        ){
                    prizeId = 2;
                    prizeDetail = "“人民健康”app健康商城积分5000";
                    prizeResult = 1;
                    hasGetPrize = true;
                    Integer[] arr = {90,234};
                    jiaodu =  arr[new Random().nextInt(2)];
                }
            }
        }

        /**
         * 三等奖
         * 5月18日至5月30日，每天出奖8个，5月31日至7月18日每天出奖4个，出奖方式为随机
         */
        if(!hasGetPrize){
            if(canGetPrizeRank == 3 || canGetPrizeRank == 15 || canGetPrizeRank == 24 || canGetPrizeRank == 35){
                prizeId = 3;
                prizeDetail = "“人民健康”app健康商城积分1000";
                prizeResult = 1;
                hasGetPrize = true;
                Integer[] arr = {162,306};
                jiaodu =  arr[new Random().nextInt(2)];
            }
        }

        if(StringUtil.isBlank(prizeDetail)){
            prizeId = 999;
            prizeDetail = "谢谢您的参与";
            prizeResult = 0;
            Integer[] arr = {54, 125, 198, 270, 342};
            jiaodu =  arr[new Random().nextInt(5)];
        }


        if(0<=prizeId && prizeId <=3){
            setOpenId2Prize(openId);
        }


        PrizeList prizeList = new PrizeList();
        prizeList.setOpenId(learnHealth.getOpenId());
        prizeList.setPid("0");
        prizeList.setTypeId(typeId);
        prizeList.setPrizeId(prizeId);//一等奖
        prizeList.setPrizeResult(prizeResult);
        prizeList.setPrizeDetail(prizeDetail);
        prizeList.setPrizeSecrect(StringUtil.randomSecret());
        prizeList.setPrizeDate(DateUtil.nowAsDate());
        prizeList.setTypeName(typeName);
        prizeList.setBizId(bizId);
        prizeList.setJiaodu(jiaodu);
        prizeList = this.insert(prizeList);
        return prizeList;

    }

    private PrizeList satisfyQuestionDoDraw1(Integer bizId) {
        Date now = DateUtil.nowAsDate();
        String nowDateFmt = DateUtil.formatNow(DateUtil.DATE_YYYYMMDD);
        SatisfyQuestion satisfyQuestion = satisfyQuestionService.fetchById(bizId);
        int doPrizeStatus = satisfyQuestion.getDoPrizeStatus();
        String pid = satisfyQuestion.getPid();
        String openId = satisfyQuestion.getOpenId();

        int typeId = ActivityEnum.SATISFY_QUESTION.getActivityId();
        int prizeId = 0;
        String prizeDetail = "",typeName = ActivityEnum.SATISFY_QUESTION.getName();
        int prizeResult = 0,jiaodu = 0;

        boolean hasGetPrize = false;

        if(StringUtil.isBlank(pid)){//无保单的情况
            if(isGetPrizeByOpenId(openId)){
                prizeId = 999;
                prizeDetail = "谢谢您的参与";
                prizeResult = 0;
                hasGetPrize = true;//代表抽奖已结束
                Integer[] arr = {54, 125, 198, 270, 342};
                jiaodu =  arr[new Random().nextInt(5)];
            }
        }else{//有保单的情况
            if(isGetPrizeByPid(pid)){
                prizeId = 999;
                prizeDetail = "谢谢您的参与";
                prizeResult = 0;
                hasGetPrize = true;//代表抽奖已结束
                Integer[] arr = {54, 125, 198, 270, 342};
                jiaodu =  arr[new Random().nextInt(5)];
            }
        }


        if(hasGetPrize == false && (doPrizeStatus == 1 || StringUtil.isBlank(pid))){//具备抽奖资格
            setOpenId2Prize(openId);
            setPid2Prize(pid);
            redisManager.incr("prize:satisfy:canGetPrize:"+nowDateFmt);
        }


        String canGetPrizeRankStr = redisManager.get("prize:satisfy:canGetPrize:"+nowDateFmt);
        int canGetPrizeRank = 1;
        if(null != canGetPrizeRankStr && StringUtil.isNumeric(canGetPrizeRankStr)){
            canGetPrizeRank = Integer.parseInt(canGetPrizeRankStr);
        }

        //具备特等 一等 二等中奖资质
        if(!hasGetPrize && doPrizeStatus == 1){

            /**
             * 特等奖
             * 5月21日、6月19日、7月17日每日1个，出奖方式为每天参与本活动的符合中奖条件的第6位用户
             */
            if(DateUtil.isCurrentDay(2017,5,21) || DateUtil.isCurrentDay(2017,6,19) || DateUtil.isCurrentDay(2017,7,17)){
                if(canGetPrizeRank == 6){
                    prizeId = 0;
                    prizeDetail = "小米室内空气净化器";
                    prizeResult = 1;
                    hasGetPrize = true;
                    jiaodu = 18;
                }
            }


            /**
             * 一等奖处理
             * 5月18日，5月20日，5月22日，5月24日，5月26日，5月28日，5月30日， 6月1日，6月4日，6月7日，6月9日，6月11日，6月13日，6月16日，
             * 6月18日，6月20日，6月22日，6月24日，6月26日，6月28日，，6月29日，7月1日，7月3日，7月5日，7月8日，7月10日，7月12日，7月14日，7月16日，
             * 7月18日，每日一个，出奖方式为每日参与本活动的符合条件的第10位用户中奖
             */
            if(!hasGetPrize){
                if(canGetPrizeRank == 10){
                    if(DateUtil.isCurrentDay(2017,5,18) ||
                            DateUtil.isCurrentDay(2017,5,20)||
                            DateUtil.isCurrentDay(2017,5,22)||
                            DateUtil.isCurrentDay(2017,5,24)||
                            DateUtil.isCurrentDay(2017,5,26)||
                            DateUtil.isCurrentDay(2017,5,28)||
                            DateUtil.isCurrentDay(2017,5,30)||
                            DateUtil.isCurrentDay(2017,6,1)||
                            DateUtil.isCurrentDay(2017,6,4)||
                            DateUtil.isCurrentDay(2017,6,7)||
                            DateUtil.isCurrentDay(2017,6,9)||
                            DateUtil.isCurrentDay(2017,6,11)||
                            DateUtil.isCurrentDay(2017,6,13)||
                            DateUtil.isCurrentDay(2017,6,16)||
                            DateUtil.isCurrentDay(2017,6,18)||
                            DateUtil.isCurrentDay(2017,6,20)||
                            DateUtil.isCurrentDay(2017,6,22)||
                            DateUtil.isCurrentDay(2017,6,24)||
                            DateUtil.isCurrentDay(2017,6,26)||
                            DateUtil.isCurrentDay(2017,6,28)||
                            DateUtil.isCurrentDay(2017,6,29)||
                            DateUtil.isCurrentDay(2017,7,1)||
                            DateUtil.isCurrentDay(2017,7,3)||
                            DateUtil.isCurrentDay(2017,7,5)||
                            DateUtil.isCurrentDay(2017,7,8)||
                            DateUtil.isCurrentDay(2017,7,10)||
                            DateUtil.isCurrentDay(2017,7,12)||
                            DateUtil.isCurrentDay(2017,7,14)||
                            DateUtil.isCurrentDay(2017,7,16)||
                            DateUtil.isCurrentDay(2017,7,18)
                            ){
                        prizeId = 1;
                        prizeDetail = "小米空气PM2.5检测仪霾表";
                        prizeResult = 1;
                        hasGetPrize = true;
                        jiaodu = 234;
                    }
                }
            }


            /**
             * 二等奖处理
             * 5月18日-6月24日每日出奖2个，出奖方式为每日参与本活动的符合中奖条件的第20与第30位用户；
             */
            if(!hasGetPrize) {
                if (canGetPrizeRank == 20 || canGetPrizeRank == 30) {
                    if(DateUtil.isInBetweenDate(DateUtil.newDate(2017,5,18),DateUtil.newDate(2017,6,25))){
                        prizeId = 2;
                        prizeDetail = "黑科技智能电源蓝牙音响";
                        prizeResult = 1;
                        hasGetPrize = true;
                        jiaodu = 90;
                    }
                }
            }

            /**
             * 二等奖处理
             * 6月25日-7月18日每日出奖1个，出奖方式为每日参与本活动的符合中奖条件的第9位用户
             */
            if(!hasGetPrize) {
                if (canGetPrizeRank == 9) {
                    if(DateUtil.isInBetweenDate(DateUtil.newDate(2017,6,25),DateUtil.newDate(2017,7,19))){
                        prizeId = 2;
                        prizeDetail = "黑科技智能电源蓝牙音响";
                        prizeResult = 1;
                        hasGetPrize = true;
                        jiaodu = 90;
                    }
                }
            }
        }


        /**
         * 三等奖
         * 5月18日至5月30日，每天出奖8个，5月31日至7月18日每天出奖4个，出奖方式为随机。
         */
        if(!hasGetPrize && now.after(DateUtil.newDate(2017,5,18)) && now.before(DateUtil.newDate(2017,5,31))){//5月18日至5月30日，每天出奖8个
            String todayCountStr = redisManager.get("prize:satisfy:"+nowDateFmt);
            if(null != todayCountStr && Integer.parseInt(todayCountStr) <8 && new Random().nextInt(50)==20){
                prizeId = 3;
                prizeDetail = "小米充电宝";
                prizeResult = 1;
                redisManager.incr("prize:satisfy:"+nowDateFmt);
                Integer[] arr = {162,306};
                jiaodu =  arr[new Random().nextInt(2)];
            }
        }else if(!hasGetPrize &&now.after(DateUtil.newDate(2017,5,31)) && now.before(DateUtil.newDate(2017,7,19))){//5月31日至7月18日每天出奖4个
            String todayCountStr = redisManager.get("prize:satisfy:"+nowDateFmt);
            if(null != todayCountStr && Integer.parseInt(todayCountStr) <4 && new Random().nextInt(50)==20){
                prizeId = 3;
                prizeDetail = "小米充电宝";
                prizeResult = 1;
                redisManager.incr("prize:satisfy:"+nowDateFmt);
                Integer[] arr = {162,306};
                jiaodu =  arr[new Random().nextInt(2)];
            }
        }



        if(StringUtil.isBlank(prizeDetail)){
            prizeId = 999;
            prizeDetail = "谢谢您的参与";
            prizeResult = 0;
            Integer[] arr = {54, 125, 198, 270, 342};
            jiaodu =  arr[new Random().nextInt(5)];
        }


        if(0<=prizeId && prizeId <=3){
            setOpenId2Prize(openId);
            setPid2Prize(pid);
        }

        PrizeList prizeList = new PrizeList();
        prizeList.setOpenId(satisfyQuestion.getOpenId());
        prizeList.setPid(satisfyQuestion.getPid());
        prizeList.setTypeId(typeId);
        prizeList.setPrizeId(prizeId);
        prizeList.setPrizeResult(prizeResult);
        prizeList.setPrizeDetail(prizeDetail);
        prizeList.setPrizeSecrect(StringUtil.randomSecret());
        prizeList.setPrizeDate(DateUtil.nowAsDate());
        prizeList.setTypeName(typeName);
        prizeList.setBizId(bizId);
        prizeList.setJiaodu(jiaodu);
        prizeList = this.insert(prizeList);
        return prizeList;
    }

    public List<PrizeList> fetchPrizeListByPid(String pid, String openId) {
        Sql sql = Sqls.create("select * from prizeList where openId = @openId order by prizeDate desc");
        sql.setParam("openId",openId);
        List<PrizeList> prizeLists = this.list(sql);
        return prizeLists;
    }

    public List<PrizeList> fetchAllPrize() {
        Sql sql = Sqls.create("select * from prizeList where prizeId in (1,2,3,4,0) and phoneNum <> ''");
        List<PrizeList> prizeLists = this.list(sql);
        return prizeLists;
    }

    public void setOpenId2Prize(String openId){
        if(StringUtil.isNotBlank(openId)){
            redisManager.sadd("allGetPrizeUserCount:openId",openId);
        }
    }

    public boolean isGetPrizeByOpenId(String openId){
        if(StringUtil.isBlank(openId)){
            return false;
        }
        return redisManager.sismember("allGetPrizeUserCount:openId",openId);
    }

    public void setPid2Prize(String pid){
        if(StringUtil.isNotBlank(pid)){
            redisManager.sadd("allGetPrizeUserCount:pid",pid);
        }

    }
    public boolean isGetPrizeByPid(String pid){
        if(StringUtil.isBlank(pid)){
            return false;
        }
        return redisManager.sismember("allGetPrizeUserCount:pid",pid);
    }


    public void setInsurancePrize(String pid){
        if(StringUtil.isNotBlank(pid)){
            redisManager.sadd("prize:in:insurance:pid",pid);
        }
    }
    public boolean isInInsurancePrize(String pid){
        if(StringUtil.isBlank(pid)){
            return false;
        }
        return redisManager.sismember("prize:in:insurance:pid",pid);
    }

    public Long getAllPrizedUserCount(int typeId){
        Sql sql = null;
        if(0 == typeId){
            sql = Sqls.create("select count(1) from prizeList where prizeId >=0 and prizeId <= 4");
        }else{
            sql = Sqls.create("select count(1) from prizeList where typeId = @typeId and prizeId >=0 and prizeId <= 4");
            sql.setParam("typeId",typeId);
        }
        return this.count(sql);
    }

    public Long getPrizedUserCountByDate(int typeId,Date date){
        Sql sql = null;
        if(0 == typeId){
            sql = Sqls.create("select count(1) from prizeList where prizeId >=0 and prizeId <= 4 and prizeDate like @prizeDate");
            sql.setParam("prizeDate",DateUtil.format(date,DateUtil.DATE_YYYY_MM_DD)+"%");
        }else{
            sql = Sqls.create("select count(1) from prizeList where typeId = @typeId and  prizeId >=0 and prizeId <= 4 and prizeDate like @prizeDate");
            sql.setParam("typeId",typeId);
            sql.setParam("prizeDate",DateUtil.format(date,DateUtil.DATE_YYYY_MM_DD)+"%");
        }

        return this.count(sql);
    }

    public DataTableResult<PrizeList> getAllPrizedData(DataTableRequest dataTableRequest,int typeId){

        Sql sql = null;
        if(0 == typeId){
            sql = Sqls.create("select * from prizeList where prizeId >=0 and prizeId <= 4 order by prizeId asc");
        }else{
            sql = Sqls.create("select * from prizeList where typeId = @typeId and prizeId >=0 and prizeId <= 4 order by prizeId asc");
            sql.setParam("typeId",typeId);
        }
        return this.data(dataTableRequest,sql);
    }
}
